package com.iris.live.services.report.data.baseline;

import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.ReportDataConvertor;
import com.iris.live.services.report.data.formulars.FormularFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lichao on 2017/9/6.
 */
@Component("OtherBaselineConvertor")
public class OtherBaselineConvertor  extends ReportDataConvertor<BaselineContext> {
    public OtherBaselineConvertor() {
    }

    @Override
    protected void getData(BaselineContext context, Map reportRow, Map resultSet, ReportConfig config) throws Exception {
        Map resultCell = null;
        String configCode = config.getValueKey(), fullPath = this.getFullPath(config);
        Double value = null;

        if(resultSet.get(configCode) != null){
            value = Double.parseDouble(resultSet.get(configCode).toString());
            if(config.getRemark()!=null){
                value = value/Double.parseDouble(config.getRemark());
            }
        }else{
            value = 0d;
        }
        List<BaseUnit> units = context.getUnits();
        boolean available = false;
        boolean flag = false;
        boolean flagByNormal = false;
        for(BaseUnit unit : units){
            resultCell = (Map)reportRow.get(fullPath+"_"+unit.getValue());
            if(resultCell == null){
                resultCell = new HashMap<>();
                resultCell.put("color", 0);
                resultCell.put("displayRow", unit.getValue());
                resultCell.put("displayRowOrder", unit.getDisplayRowOrder());

                this.setResultCell(resultCell, config, 0);
            }
            flagByNormal = unit.getType() == BaseUnit.BaseType.NORMAL;
            if(flagByNormal){
                if(config.getAinCode().equals(resultSet.get(unit.getKey()).toString())){
                    this.setResultCell(resultCell, config, value);
                }
            }else {
                available = unit.getValue().equals(resultSet.get(unit.getKey()));
                if (available) { //满足Y轴处理条件，则进行数据汇总
                    this.setResultCell(resultCell, config, value);
                    flag = true;
                }
                //如果为总计,则累加
                if (unit.getValue().equals("总计")) {
                    this.setResultCell(resultCell, config, value);
                }
            }
            reportRow.put( fullPath+"_"+unit.getValue(), resultCell);
        }
        if(!flagByNormal) {
            if (!flag) {
                resultCell = (Map) reportRow.get(fullPath + "_" + "其他");
                this.setResultCell(resultCell, config, value);
            }
        }
    }

    @Override
    protected void getFormularData(BaselineContext context, Map<String, Map> datas, Map reportRow, ReportConfig config) throws Exception {
        String fullPath = this.getFullPath(config), formularExpression = config.getFormularExpression();
        Map resultCell = null;
        String formular = formularExpression.substring(0, formularExpression.indexOf("("));
        List<BaseUnit> units = context.getUnits();
        for(BaseUnit unit : units){ //按照用户设置的基础轴上的数据和当前数据进行比较并进行合计
            resultCell = (Map) reportRow.get(fullPath + "_" + unit.getValue());
            if (resultCell == null) {
                resultCell = new HashMap<>();
                resultCell.put("color", 0);
                resultCell.put("displayRow", unit.getValue());
                resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                resultCell.put(config.getCategory() + "Value", 0);
            }

            Double value = FormularFactory.getFormular(formular).getValue(config, unit.getValue(), datas);
            this.setResultCell(resultCell, config, value);
            reportRow.put(fullPath + "_" + unit.getValue(), resultCell);
        }
    }
}
